package com.vsoft.sms_cm;

import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
import com.vsoft.anti_fra.*;

public class RedoCRMInterface {

	private static Logger vLogger = Logger.getLogger(RedoCRMInterface.class);
	private static String vName = RedoCRMInterface.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	
	
	public static void main(String[] args)  {
	
		try{

			HashMap<String,String> outCols	=	new HashMap<String,String> ();
			{
				outCols.put("LOG_TIME", "1");
				outCols.put("LOG_BIZSF_SN", "1");
				outCols.put("LOG_STATE", "1");
				outCols.put("LOG_DESC", "1");
				outCols.put("UPDATE_LOG_TIME", "1");
				outCols.put("AREA_CODE", "1");
				//outCols.put("SYS_SRC", "1");
				outCols.put("LOG_BIZSU_SN", "1");
				outCols.put("AREA_CODE", "1");
				outCols.put("LOG_BIZSU_SN", "1");
				
			}
			vLogger.info("---------------InitSystem-------------");
			Configuration.InitSystem();
			vLogger.info("---------------generate test data-------------");

			//RedoCRMInterface t 				= 	new RedoCRMInterface();
			Connection conn 				= 	DBTrans.getConnection();
			String 	redoMode				=	args[0];
			String 	redoDays				=	args[1];
			if(CmPublic.isStrNullOrEmpty(redoMode))
				redoMode					=	"1";
			if(CmPublic.isStrNullOrEmpty(redoDays))
				redoDays					=	"1";
				//throw new BaseException(vName+"0001","redoMode为空");
			//+LogCollect.getStackLog(e)
			String	serviceName				=	"";
			if(redoMode.trim().equals("1"))
				serviceName					=	"BizSF";
			else if(redoMode.trim().equals("2"))
				serviceName					=	"BizSU";
			else
				throw new BaseException(vName+"0001","不支持的redoMode（1 or 2）："+redoMode);
			String logTable					=	"log_"+serviceName;
			String	sql						=	"select * from "+logTable +
												" where log_state=2 and order_num in('2810667','2811118','7016120','7016128','7023682','8664938')" +
/*												" where log_state='-1' and log_time >sysdate -"+redoDays+
												//" and log_desc  like '%找不到区域编码%' and sys_src='2' " +
												" and log_bizsf_sn = 28473" +*/
												" order by log_time";
			//sql	=	"select order_num,area_no,'A' sys_src  from reprocess_20120521 ";
			SQLArgs	 vArg					= 	new SQLArgs();
			vArg.reset();
			BusiObj rsBo 					= 	OperAgent.execQuery(conn, "Query redo info " ,sql, vArg);
			vLogger.info("查询crm redo信息，返回数据是:\n" + BOHelper.boToXML(rsBo));
			String 		listName			=	"RESULT";
			ArrayList<BusiObj> alRedoInfo 	= 	rsBo.getBusiObjList(listName);
			int		cnt						=	alRedoInfo.size();
			if(cnt>0)
			{
				int	i						=	1;
				for(BusiObj m:alRedoInfo)
				{
					BusiObj	servBo			=	new BusiObj();
					servBo.setConnection(conn);
					servBo.setName(serviceName);
					servBo.setString("debugSee", "1");
					HashMap<String, Object> one	=		m.getRawData();
					HashMap<String, String> oneRow	=		new HashMap<String, String>();
					for(String key:one.keySet())
					{
						String flag	=	outCols.get(key.toUpperCase());
						if(flag==null)
						{
							oneRow.put(key, (String)one.get(key));
							servBo.setString(key, (String)one.get(key));
						}
					}
					//for(String)
					BusiObj retBo			=	ServiceBus.runService(servBo);
					vLogger.info("================redo info====================共 "+cnt+"条接口需重处理，\n第"
								+i+"  条重处理返回数据是:\n" + BOHelper.boToXML(retBo));
					i++;
				}
			}
			else
			{
				vLogger.info("没有可以重处理的接口数据，重处理程序正常退出！");
				return;
			}
			

			conn.commit();
			conn.close();
			//vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(retBo));

			//////////////////////////////

		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		catch(BaseException e)
		{
			e.printStackTrace();
			vLogger.error(e.ErrMethod);
			vLogger.error(e.SysMsg);
		}
			vLogger.info("---------------Game Over-------------");
	}	
	
		

	private int  init()
	{
		int iRet = 0;
		vSD = new  SessionData();
		vSD.vRoleCode = new String[] {"1"};
		vSD.vStaffCode = "1";
		vSD.vStaffNo = "0001";
		vSD.vStaffName = "test";
		
	
		vDebugHome = System.getProperty("DEBUG_HOME");
		if (vDebugHome == null)
		{
			iRet = -1;
			vLogger.error("需要设置DEBUG_HOME环境变量来指定debug 文件所在的目录");
		}
		return iRet;
		
	}
	
}
